clear
macro drop _all 
set more off

* specify where regression sample is saved
global direc_sample "..."

* specify where regression estimates will be saved
global direc_estimates "..."

* specify where regression output figures will be saved
global direc_output "..."

* window size around the year when the regulation is introduced
global wsize = 2

* import data
import delimited "${direc_sample}/regression_sample.csv", clear

* define treatment var
do _create-treatment-var "state_id"

* labels
run _labels

*** regression analysis
gen never_treat=(reg_year==3000)

gen ry=year-reg_year

        forvalues k = 18(-1)1 {
           gen g_`k' = ry == -`k'
        }
        forvalues k = 0/18 {
             gen g`k' = ry == `k'
        }

gen reg_year_alt=reg_year
replace reg_year_alt=. if reg_year_alt==3000

gen event_pre2=(year_post_reg==0)
gen event_pre1=(year_post_reg==1)
gen event_post0=(year_post_reg==2)
gen event_post1=(year_post_reg==3)
gen event_post2=(year_post_reg==4)

global DepVars "plans_making_nonzero_new_sales insurers_making_nonzero_new_sale  avg_prems_new_sale"

foreach depvar of varlist $DepVars {

	did_imputation `depvar'  state_id year reg_year_alt, fe(state_id year) cluster(state_id) hor(0/2) pretrends(2) minn(0) 
	
	event_plot, default_look graph_opt(xtitle("Years since the regulation", size(vlarge)) title(`: variable label `depvar'', size(vlarge)) xlabel(, labsize(large)) ylabel(, labsize(large))) stub_lag(tau#) stub_lead(pre#) together alpha(0.10)

	est save "${direc_estimates}/BJS_`depvar'-dynamic", replace

	sum `depvar' if e(sample)
		
}

*** regression output figures
run _plot_coeff_BJS "${DepVars}" 
